package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.entity.TGoodVarietyTbl;
import com.dom.domweb.dao.entity.TMeasureUnitTbl;
import com.dom.domweb.dao.mapper.TMeasureUnitTblMapper;
import com.dom.domweb.dao.service.ITMeasureUnitTblService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * B_计量单位代码表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TMeasureUnitTblServiceImpl extends ServiceImpl<TMeasureUnitTblMapper, TMeasureUnitTbl> implements ITMeasureUnitTblService {

    @Override
    public B139A getUnitInfoList(B139R request) {
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper();
//        wrapper.notIn("stat", PubStatEnum.STAT_STOP.getCode());
        Page<TMeasureUnitTbl> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());
        page = this.selectPage(page,wrapper);
        B139A result = new B139A();
        result.setTotalNum((int)page.getTotal());
        List<UnitInfo> list = infoToTbl(page.getRecords());
        result.setUnitData(list);
        return result;
    }

    public Map<String,Object> getAllUnitInMap(){
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper();
        List<TMeasureUnitTbl> list = this.baseMapper.selectList(wrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        for (TMeasureUnitTbl tbl: list) {
            map.put(tbl.getUnitCode().toString(),tbl.getUnit());
        }
        return map;
    }
    private List<UnitInfo> infoToTbl(List<TMeasureUnitTbl> list) {
        List<UnitInfo> result = new ArrayList<>();
        for (TMeasureUnitTbl tblInfo:  list) {
            UnitInfo info = new UnitInfo();
            info.setId(tblInfo.getId().toString());
            info.setUnit(tblInfo.getUnit());
            info.setMemCode(tblInfo.getMemCode());
            info.setStat(tblInfo.getStat().toString());
            info.setUnitName(tblInfo.getUnitName());
            info.setUnitCode(tblInfo.getUnitCode());
            info.setStatName(PubStatEnum.getName(tblInfo.getStat()));
            result.add(info);
        }
        return result;
    }

    @Override
    public B139A getAllUnitInfo() {
        B139A b = new B139A();
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper();
        List<TMeasureUnitTbl> list = this.baseMapper.selectList(wrapper);
        List<UnitInfo> result = infoToTbl(list);
        b.setUnitData(result);
        return b;
    }

    @Override
    public B133A getUnitInfoByCode(String id) {
        TMeasureUnitTbl tbl =  this.baseMapper.selectById(id);
        B133A result = new B133A();
        result.setUnit(tbl.getUnit());
        result.setMemCode(tbl.getMemCode());
        result.setId(tbl.getId().toString());
        result.setUnitCode(tbl.getUnitCode() == null ? null : tbl.getUnitCode().toString());
        result.setUnitName(tbl.getUnitName());
        result.setStat(tbl.getStat().toString());
        return result;
    }

    @Override
    public RspModel insertUnitInfo(B130R request) {
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper<TMeasureUnitTbl>();
        wrapper.eq("unit_code",request.getUnitCode());
        List<TMeasureUnitTbl> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0){
            return new RspModel("RSP000032","商品单位已存在！");
        }
        TMeasureUnitTbl tblInfo = new TMeasureUnitTbl();
        tblInfo.setUnit(request.getUnit());
        tblInfo.setStat(PubStatEnum.STAT_UNACTIVATED.getCode());
        tblInfo.setMemCode(0);
        tblInfo.setUnitCode(StringUtils.isEmpty(request.getUnitCode()) ? null : Integer.valueOf(request.getUnitCode()));
        tblInfo.setUnitName(request.getUnitName());
        tblInfo.setVersion(0);
        int count = this.baseMapper.insert(tblInfo);
        RspModel model = new RspModel("RSP000000","商品单位信息新增成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel editUnitInfo(B130R request) {
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper<TMeasureUnitTbl>();
        wrapper.eq("id",request.getId());
        TMeasureUnitTbl tbl = this.baseMapper.selectById(request.getId());
        if (tbl.getStat() != PubStatEnum.STAT_UNACTIVATED.getCode()
                &&  !request.getUnitCode().equals(Integer.toString(tbl.getUnitCode())) ){
            return new RspModel(ConstantError.NOT_CODE_ERROR);
        }
        TMeasureUnitTbl tblInfo = new TMeasureUnitTbl();
        tblInfo.setUnit(request.getUnit());
        tblInfo.setUnitCode(StringUtils.isEmpty(request.getUnitCode()) ? null : Integer.valueOf(request.getUnitCode()));
        tblInfo.setUnitName(request.getUnitName());
        tblInfo.setStat(StringUtils.isEmpty(request.getStat()) ? PubStatEnum.STAT_UNACTIVATED.getCode() : Integer.valueOf(request.getStat()));
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品单位信息修改成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel unitInfoDel(String id) {
        Wrapper<TMeasureUnitTbl> wrapper = new EntityWrapper<TMeasureUnitTbl>();
        wrapper.eq("id",id);
        TMeasureUnitTbl tblInfo = new TMeasureUnitTbl();
        tblInfo.setStat(PubStatEnum.STAT_STOP.getCode());
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品单位信息删除成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }
}
